package com.xiaoxin.yixinai.dict;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiaoxin.yixinai._frame.base.BaseRepository;
import com.xiaoxin.yixinai._frame.base.BaseServiceProxy;

@Service
@Transactional
public class DictService extends BaseServiceProxy<Dict> {

	@Resource
	private DictRepository dictRepository;

	@Override
	protected BaseRepository<Dict> getBaseRepository() {
		return dictRepository;
	}

	/**
	 * @param infoType
	 * @return
	 */
	public List<Dict> findListByInfoType(String infoType) {
		return dictRepository.findByInfoTypeAndIsDelete(infoType, 0);
	}

	/**
	 * 根据字典编码
	 * 
	 * @param infoType
	 * @param sysTag
	 * @return
	 */
	public List<Dict> findListByInfoTypeAndSysTag(String infoType, Integer sysTag) {
		return dictRepository.findByInfoTypeAndSysTagAndIsDeleteOrderByInfoOrderAsc(infoType, sysTag, 0);
	}

	/**
	 * 查询公司下的数据字典
	 * @param infoType
	 * @param sysTag
	 * @param companyId 公司ID
	 * @return
	 */
	public List<Dict> findByInfoTypeAndSysTagAndIsDeleteAndCompanyIdOrderByInfoOrderAsc(String infoType, Integer sysTag,
			Integer companyId) {
		return dictRepository.findByInfoTypeAndSysTagAndIsDeleteAndCompanyIdOrderByInfoOrderAsc(infoType, sysTag, 0,
				companyId);
	}

	/**
	 * 根据分类编码查询，并封装成map。 key是value值，value是名称
	 * 
	 * @param infoType
	 * @return
	 */
	public Map<String, String> findMapByInfoType(String infoType) {
		Map<String, String> map = new HashMap<String, String>();
		List<Dict> dictlist = dictRepository.findByInfoTypeAndSysTagAndIsDeleteOrderByInfoOrderAsc(infoType, 1, 0);
		dictlist.forEach(dict -> {
			map.put(dict.getValue().toString(), dict.getName());
		});
		return map;
	}
	
	/**
	 * 初始化公司的CRM配置中数据字典
	 * @param companyId
	 */
	public void initCompanyCrmDict(Integer companyId) {
		// 初始化父级 
		String[] parentInfoTypeArr = {"followUpMethod", "crm_field_select_01", "crm_field_select_03", "crm_field_select_04"
				, "crm_field_select_05", "crm_field_select_06", "crm_field_select_07", "crm_field_select_02"
				, "field_select_08"};
		
		for (int i = 0; i < parentInfoTypeArr.length; i++) {
			Dict dictParent = new Dict();
			dictParent.setCompanyId(companyId);
			dictParent.setInfoOrder(1);
			dictParent.setInfoType(parentInfoTypeArr[i]);
			dictParent.setIsDelete(0);
			dictParent.setName("CRM自定义字段");
			dictParent.setSysTag(-1);
			dictParent.setValue(0);
			dictRepository.save(dictParent);
		}
		
		// 初始化跟进方式子级
		String infoType = "followUpMethod";
		String[] nameArr = {"AI初筛", "上门拜访", "电话联系", "微信联系", "邮件联系", "到店面谈"};
		int[] revisabilityArr = {0, 0, 0, 1, 1, 1};
		this.initFieldSelectDict(companyId, infoType, nameArr, revisabilityArr);
		
		// 初始化客户状态子级
		infoType = "crm_field_select_01";
		nameArr = new String[]{"意向客户", "潜在客户", "成交客户", "流失客户", "无效客户", "失败客户"};
		revisabilityArr = new int[]{0, 0, 0, 0, 0, 1};
		this.initFieldSelectDict(companyId, infoType, nameArr, revisabilityArr);
		
		// 初始化客户来源子级
		infoType = "crm_field_select_07";
		nameArr = new String[]{"AI初筛", "大数据爬取", "批量导入", "手工录入", "扫楼", "主动联系", "朋友介绍"};
		revisabilityArr = new int[]{0, 0, 0, 0, 1, 1, 1};
		this.initFieldSelectDict(companyId, infoType, nameArr, revisabilityArr);
		
		// 初始化客户成熟度子级
		infoType = "crm_field_select_06";
		nameArr = new String[]{"明确成交意向", "有初步成交意向", "关键人有成交意向", "关键人成交意向不确定", "初步联系，有意向", "初步联系，意向不确定", "新建客户", "明显拒绝", "无效客户"};
		revisabilityArr = new int[]{1, 1, 1, 1, 1, 1, 1, 1, 1};
		this.initFieldSelectDict(companyId, infoType, nameArr, revisabilityArr);
		
		// 初始化所属行业
		infoType = "crm_field_select_02";
		nameArr = new String[]{"机械及行业设备", "电子元器件", "礼品、工艺品、饰品", "家居用品", "电工电气", "建筑、建材", "五金、工具", "纺织、皮革", "包装", "橡塑", "化工", "服装", "办公、文教", "冶金矿产", "数码、电脑", "服饰", "运动、休闲", "传媒、广电", "精细化学品", "安全、防护", "照明工业", "仪器仪表", "农业", "汽摩及配件", "通信产品", "环保", "食品、饮料", "加工", "玩具", "家用电器", "交通运输", "医药、保养", "商务服务", "印刷", "能源", "代理", "纸业", "其它行业", "母婴培训", "企业风险评估", "作房产中介"};
		revisabilityArr = new int[]{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
		this.initFieldSelectDict(companyId, infoType, nameArr, revisabilityArr);
		
		// 初始化客户类别子级
		infoType = "crm_field_select_04";
		nameArr = new String[]{"普通客户", "VIP客户", "老客户", "供货商", "合作伙伴", "运营中心", "省级代理", "市级代理", "普通代理"};
		revisabilityArr = new int[]{1, 1, 1, 1, 1, 1, 1, 1, 1};
		this.initFieldSelectDict(companyId, infoType, nameArr, revisabilityArr);
		
		// 初始化客户等级子级
		infoType = "crm_field_select_05";
		nameArr = new String[]{"A", "B", "C", "D", "E", "F"};
		revisabilityArr = new int[]{1, 1, 1, 1, 1, 1};
		this.initFieldSelectDict(companyId, infoType, nameArr, revisabilityArr);
		
		// 初始化意向产品子级
		infoType = "crm_field_select_03";
		nameArr = new String[]{"电话机器人", "赢客CRM", "微信名片", "数据爬虫"};
		revisabilityArr = new int[]{1, 1, 1, 1};
		this.initFieldSelectDict(companyId, infoType, nameArr, revisabilityArr);
		
		// 初始化联系人性别
		infoType = "field_select_08";
		nameArr = new String[]{"男", "女"};
		revisabilityArr = new int[]{0, 0};
		this.initFieldSelectDict(companyId, infoType, nameArr, revisabilityArr);
	}
	
	/**
	 * 初始化子级
	 * @param companyId
	 * @param infoType
	 * @param nameArr
	 * @param revisabilityArr
	 */
	private void initFieldSelectDict(Integer companyId, String infoType
			, String[] nameArr, int[] revisabilityArr) {
		
		if (nameArr == null 
				|| revisabilityArr == null 
				|| nameArr.length != revisabilityArr.length) {
			throw new RuntimeException("数组信息不对应");
		}
		
		List<Dict> list = new LinkedList<Dict>();
		for (int i = 0; i < nameArr.length; i++) {
			Dict dict = new Dict();
			dict.setCompanyId(companyId);
			dict.setInfoOrder(i+1);
			dict.setInfoType(infoType);
			dict.setIsDelete(0);
			dict.setName(nameArr[i]);
			dict.setRevisability(revisabilityArr[i]);
			dict.setSysTag(1);
			dict.setValue(i+1);
			
			list.add(dict);
		}
		
		dictRepository.save(list);
	}
}
